﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Interfaces.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;

	/**
	 *	对比PositionedObject， 主要多了children, 
	 */
    public class ObjectLayer extends PositionedObject implements IEngineObject, IRenderObject {

        protected var m_children:Array;
        public var priority:Number;
        public var name:String;

        public function ObjectLayer(_arg1:String, _arg2:Number=0){
            this.m_children = [];
            super();
            this.name = _arg1;
            this.priority = _arg2;
            this.m_displayObject = new Sprite();
        }
        public function get children():Array{
            return (this.m_children);
        }
        public function cleanUp():void{
            var _local3:WorldObject;
            var _local1:Array = this.m_children.concat([]);
            var _local2:int;
            while (_local2 < _local1.length) {
                _local3 = (_local1[_local2] as WorldObject);
                if (_local3 != null){
                    _local3.detach();
                    _local3.cleanUp();
                };
                _local2++;
            };
            m_displayObject = new Sprite();
        }
        public function update():void{
            var _local1:int;
            while (_local1 < this.m_children.length) {
                WorldObject(this.m_children[_local1]).update();
                _local1++;
            };
        }
        public function attach():void{
            var _local1:IsoViewport = IsoViewport(GlobalEngine.viewport);
            _local1.addObjectLayer(this);
        }
        public function detach():void{
            var _local1:IsoViewport = IsoViewport(GlobalEngine.viewport);
            _local1.removeObjectLayer(this);
        }
        public function render(_arg1:RenderContext):void{
            var _local2:int = this.m_children.length;
            var _local3:int;
            while (_local3 < _local2) {
                WorldObject(this.m_children[_local3]).render(_arg1);
                _local3++;
            };
        }
		/**
		 * 把物品放到这个图层中，并且按深度层次摆放好
		 */
        public function insertObjectIntoDepthArray(_arg1:WorldObject, _arg2:String=null):void{
            var _local3:int = this.binarySearch(_arg1);
            this.m_children.splice(_local3, 0, _arg1);
            var _local4:DisplayObject = _arg1.getDisplayObject();
            var _local5:Sprite = Sprite(getDisplayObject());
            if (_local4 == null){
                _local5.addChildAt(_arg1.createDisplayObject(), _local3);
            } else {
                if (_local4.parent == null){
                    _local5.addChildAt(_local4, _local3);
                } else {
                    _local4.parent.setChildIndex(_local4, _local3);
                };
            };
        }
		/**
		 * 更新物品在这个图层中的深度层次 
		 */
        public function updateObjectInDepthArray(_arg1:WorldObject):void{
            var _local2:DisplayObject;
            var _local3:int;
            var _local4:int;
            if (_arg1.isAttached()){
                _local2 = _arg1.getDisplayObject();
                if (((_local2) && (_local2.parent))){
                    _local3 = this.m_children.indexOf(_arg1);
                    if (_local3 != -1){
                        this.m_children.splice(_local3, 1);
                    };
                    _local4 = this.binarySearch(_arg1);
                    this.m_children.splice(_local4, 0, _arg1);
                    _local2.parent.setChildIndex(_local2, _local4);
                } else {
                    this.insertObjectIntoDepthArray(_arg1);
                };
            };
        }
        protected function binarySearch(_arg1:WorldObject):int{
            var _local5:Number;
            var _local2:int;
            var _local3:int = this.m_children.length;
            var _local4 = (_local3 >> 1);
            while (_local2 < _local3) {
                _local5 = this.compareWorldObjects(_arg1, WorldObject(this.m_children[_local4]));
                if (_local5 < 0){
                    _local3 = _local4;
                    _local4 = ((_local2 + _local3) >> 1);
                } else {
                    if (_local5 > 0){
                        _local2 = (_local4 + 1);
                        _local4 = ((_local2 + _local3) >> 1);
                    } else {
                        break;
                    };
                };
            };
            return (_local4);
        }
        public function checkObjectDepthLocation(_arg1:DisplayObject, _arg2:int):Boolean{
            return ((_arg1 == _arg1.parent.getChildAt(_arg2)));
        }
        protected function compareWorldObjects(_arg1:WorldObject, _arg2:WorldObject):Number{
            var _local6:Vector3;
            var _local7:Number;
            var _local8:Number;
            var _local9:Vector3;
            var _local10:Number;
            var _local11:Number;
            var _local12:Vector3;
            var _local13:Number;
            var _local14:Number;
            var _local15:Vector3;
            var _local16:Number;
            var _local17:Number;
            var _local3:Number = _arg1.getDepthPriority();
            var _local4:Number = _arg2.getDepthPriority();
            var _local5:Number = (_arg2.depthIndex - _arg1.depthIndex);
            if (_local4 == _local3){
                _local6 = _arg1.getDepthPositionNoClone();
                _local7 = _local6.x;
                _local8 = _local6.y;
                _local9 = _arg1.getEndPosition();
                _local10 = _local9.x;
                _local11 = _local9.y;
                _local12 = _arg2.getDepthPositionNoClone();
                _local13 = _local12.x;
                _local14 = _local12.y;
                _local15 = _arg2.getEndPosition();
                _local16 = _local15.x;
                _local17 = _local15.y;
                if ((((_local5 <= 0)) && ((((((((_local13 >= _local10)) && ((_local14 < _local11)))) && ((_local17 > _local8)))) || ((((((_local14 >= _local11)) && ((_local13 < _local10)))) && ((_local16 > _local7)))))))){
                    _local5 = 1;
                } else {
                    if ((((_local5 >= 0)) && ((((((((_local7 >= _local16)) && ((_local8 < _local17)))) && ((_local11 > _local14)))) || ((((((_local8 >= _local17)) && ((_local7 < _local16)))) && ((_local10 > _local13)))))))){
                        _local5 = -1;
                    };
                };
            };
            return (_local5);
        }
        public function removeObjectFromDepthArray(_arg1:WorldObject):void{
            var _local3:DisplayObject;
            var _local2:int = this.m_children.indexOf(_arg1);
            if (_local2 >= 0){
                this.m_children.splice(_local2, 1);
                _local3 = _arg1.getDisplayObject();
                if (((_local3) && (_local3.parent))){
                    _local3.parent.removeChild(_local3);
                };
            };
        }
        override public function pickObject(_arg1:Point, _arg2:uint=16777215, _arg3:uint=0):PositionedObject{
            var _local4:int;
            var _local6:PositionedObject;
            var _local5:PositionedObject;
            _local4 = (this.m_children.length - 1);
            while (_local4 >= 0) {
                _local6 = (this.m_children[_local4] as PositionedObject);
                _local5 = (_local6.pickObject(_arg1, _arg2, _arg3) as PositionedObject);
                if (_local5 != null){
                    break;
                };
                _local4--;
            };
            return (_local5);
        }

    }
}//package Engine.Classes 
